package com.apes.hr.transfer.comprehensive;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.repository.HrDao;
import com.apes.scm.masterdata.frame.repository.DeptRepository;
import com.apes.scm.rbac.model.LocalPermitItem;
import com.apes.scm.rbac.repository.LocalPermitItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Title: ComprehensiveService
 * @Description: 商家端查询HR模块相关报表权限控制
 * @author: ZhengZeXian
 * @create: 2021/2/19 14:35
 */
@Service("comprehensiveService")
public class ComprehensiveService extends DomainService {

    @Autowired
    private ComprehensiveDao comprehensiveDao;
    @Autowired
    private HrDao hrDao;
    @Autowired
    private LocalPermitItemRepository localPermitItemRepository;
    @Autowired
    private DeptRepository deptRepository;

    /**
     * 根据当前角色获取查询权限的业务部门，根据业务部门转换成行政部门 ，一个行政部门对应多个业务部门
     * @param request 当前角色ID
     * @return 行政部门列表
     */
    public com.apestech.framework.json.JSONObject getAuthBranch(SimpleRequest request){
        JSONObject param = request.getJO();
        List<LocalPermitItem> localPermitItems = localPermitItemRepository.find(request.getPostId(), "pp_search", "pt_dept");
        if (!localPermitItems.isEmpty()){
            //获取行政部门列表
            List<String> ids = new ArrayList<>();
            localPermitItems.forEach(e -> ids.add(e.getOrgId()));
            List<String> branchIds = deptRepository.findDeptByIds(ids);
            if (branchIds!=null) param.put("branchIds", branchIds);
        }
        return new com.apestech.framework.json.JSONObject(param);
    }

    /**
     * 查询工龄
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject seniority(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            if(!"".equals(branchId)){
                json.put("BRANCH_ID",branchId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询招聘时效
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject efficiency(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            json.put("BRANCH_ID",branchId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询同店同职两年
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject transferTwoYear(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            json.put("BRANCH_ID",branchId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询身份证/驾照到期提醒
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject transferOneYear(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            json.put("BRANCH_ID",branchId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询长期未晋升
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject noPromotion(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            json.put("BRANCH_ID",branchId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 试用期转正提醒
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject probation(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            json.put("BRANCH_ID",branchId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 续签合同提醒
     * @param request
     * @return
     */
    public com.apestech.framework.json.JSONObject renewContract(SimpleRequest request){
        com.apestech.framework.json.JSONObject json = new com.apestech.framework.json.JSONObject();
        try {
            String body = request.getData().toString();
            json = com.apestech.framework.json.JSON.toJSON(body);
            String branchId = comprehensiveDao.queryBranchId(request.getPostId());
            if(!"".equals(branchId)){
                json.put("BRANCH_ID",branchId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /** -------查询人员构成-----------  */
    /**
     * 查询人员性别占比
     */
    public Map<Object,Object> querySex(SimpleRequest request){
        String branchId = comprehensiveDao.queryBranchId(request.getPostId());
        List<Map> list = hrDao.querySex(StringToJSONArray(branchId));
        Map<Object,Object> map = new HashMap<>();
        list.forEach(lis->{
            map.put(lis.get("SEX"),lis.get("NUM"));
        });
        return map;
    }

    /**
     * 全日制与兼职
     */
    public List queryJobClass(SimpleRequest request){
        String branchId = comprehensiveDao.queryBranchId(request.getPostId());
        List list = hrDao.queryJobClass(StringToJSONArray(branchId));
        return list;
    }

    /**
     * 年龄占比
     */
    public List queryYear(SimpleRequest request){
        String branchId = comprehensiveDao.queryBranchId(request.getPostId());
        List list = hrDao.queryYear(StringToJSONArray(branchId));
        return list;
    }

    /**
     * 学历占比
     */
    public Map<Object,Object> queryEducation(SimpleRequest request){
        String branchId = comprehensiveDao.queryBranchId(request.getPostId());
        List<Map> list = hrDao.queryEducation(StringToJSONArray(branchId));
        Map<Object,Object> map = new HashMap<>();
        list.forEach(lis->{
            map.put(lis.get("DEGREE_ID"),lis.get("NUM"));
        });
        return map;
    }

    /**
     * 人员类型
     */
    public Map<Object,Object> queryPostGroup(SimpleRequest request){
        String branchId = comprehensiveDao.queryBranchId(request.getPostId());
        List<Map> list = hrDao.queryPostGroup(StringToJSONArray(branchId));
        Map<Object,Object> map = new HashMap<>();
        list.forEach(lis->{
            map.put(lis.get("POST_GROUP_ID"),lis.get("RETIRE"));
        });
        return map;
    }

    /** -------查询人员构成END-----------  */

    /**String 字符串 转JSONArray */
    public JSONArray StringToJSONArray(String str){
        return JSONArray.parseArray("[\"" + str + "\"]");
    }
}
